package com.pzl.mylistview;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;

import java.util.ArrayList;
import java.util.HashMap;

public class MainActivity extends AppCompatActivity {
    private ArrayList<HashMap<String, Object>> data;
    private MyListView myListView;

    private int[] imgRes = new int[]{R.drawable.a11, R.drawable.a12, R.drawable.a13, R.drawable.a14,
            R.drawable.a15, R.drawable.a16, R.drawable.a17, R.drawable.a18, R.drawable.a19,
            R.drawable.a20, R.drawable.a21, R.drawable.a22};

    private ArrayList<Bitmap> bitmaps;

    private void initDrawables() {
        Resources res = getResources();

        bitmaps = new ArrayList<Bitmap>();
        for (int i = 0; i < imgRes.length; i++) {
            bitmaps.add(BitmapFactory.decodeResource(res, imgRes[i]));
        }
    }

    private void initView() {
        myListView = (MyListView) findViewById(R.id.myListView);
    }

    private void initData() {
        data = new ArrayList<>();
        for (int i = 0; i < imgRes.length; i++) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("tv", "TEST " + i);
            map.put("iv", bitmaps.get(i));
            data.add(map);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        initDrawables();
        initView();
        initData();
        myListView.setAdapter(new MyListViewAdapter(this, data));

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (bitmaps != null) {
            for (int i = 0; i < bitmaps.size(); i++) {
                bitmaps.get(i).recycle();
            }
            bitmaps.clear();
            bitmaps = null;
        }
    }


}
